package com.erp.controller;

import com.erp.mapper.OutStockMapper;
import com.erp.pojo.Goods;
import com.erp.pojo.OutStock;
import com.erp.tool.ImportExcelUtils;
import com.erp.tool.PgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class OutStockController {

    @Autowired
    private OutStockMapper mapper;

    @Autowired
    private ImportExcelUtils excelUtils;

    @RequestMapping("/uploadOutStockExcel")
    public Map<String,Object> upload(MultipartFile excelFile) throws Exception{
        Map<String,Object> map = new HashMap<>();
        if (excelFile.isEmpty()){
            map.put("mag","文件夹为空，重新上传");
            return map;
        }else {
            String fileName = excelFile.getOriginalFilename();
            InputStream is = excelFile.getInputStream();
            List<OutStock> stockList = excelUtils.uploadOutStock(fileName, is);
            if (mapper.insertOutStock(stockList)>0){
                map.put("msg","数据添加成功");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date=simpleDateFormat.format(new Date());
                map.put("data",mapper.conditionDate(date));
                return map;
            }else {
                map.put("msg","数据添加失败，请重新添加");
                return map;
            }
        }
    }

    @RequestMapping("/limitOutStock")
    public List limitUsers(String date, int num,Integer state){
        System.out.println(state);
        List<Object>allList = new ArrayList<Object>();
        int row=4;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("date",date);
        map.put("state",state);
        map.put("end",row);
        map.put("start", PgTool.getStart(num,row).get("start"));
        //查询需多少页
        allList.add(PgTool.getPage(row, mapper.countStock(map)));
        //得到分页数据
        allList.add(mapper.findAllStock(map));
        return allList;
    }

    @RequestMapping("/updateOutStockState")
    public Map updateStockState(Integer state,Integer id){
        Map<String, Boolean>map=new HashMap<>();
        if (mapper.updateOutStockState(state, id)>0){
            map.put("state",true);
        }else {
            map.put("state",false);
        }
        return map;
    }

    @RequestMapping ("/updateOutStockStateByIds")
    public Map updateOutStockStateByIds(Integer state,String idStr){
        String[] array=idStr.split("/");
        List<Integer>ids=new ArrayList<>();
        for (int i = 1; i < array.length; i++) {
            ids.add(Integer.parseInt(array[i]));
        }
        Map<String, Boolean>map=new HashMap<>();
        for (Integer id : ids) {
            System.out.println(id);
        }
        if (mapper.updateOutStockStateByIds(state, ids)>0){
            List<OutStock> stockList = mapper.findOutStockIDs(ids);
            List<Goods>goodsList=new ArrayList<>();
            for (OutStock outStock : stockList) {
                System.out.println(outStock);
                Goods good = new Goods();
                good.setGName(outStock.getOutGoodsname());
                good.setGNum(outStock.getOutGoodsnum());
                goodsList.add(good);
            }
            map.put("state",true);
        }else {
            map.put("state",false);
        }
        return map;
    }


}
